package networkTraining;

import java.util.HashMap;
import java.util.Map;

import networkTraining.algorithms.ITrainingAlgorithm;
import neuralNetwork.INeuralNetwork;
import neuralNetwork.execution.NeuralNetworkExecutor;

public class NetworkTrainer
{
	private NetworkTrainer()
	{
	}

	public static Map<String, Double> TrainNetwork(INeuralNetwork neuralNetwork, DataSet dataSet,
	    ITrainingAlgorithm trainingAlgorithm)
	{
		Map<String, Double> errorMap = new HashMap<String, Double>();

		if(dataSet.getInputValue("green") == 0.0)
		{
			System.out.println("stop");
		}
		for (String inputName : dataSet.getInputNames())
		{
			neuralNetwork.getNeuron(inputName).setOutputValue(dataSet.getInputValue(inputName));
		}

		NeuralNetworkExecutor.performTimeSteps(neuralNetwork, neuralNetwork.getLayerCount());

		for (String outputName : dataSet.getOutputNames())
		{
			double actualOutput = neuralNetwork.getNeuron(outputName).getOutputValue();
			double expected = dataSet.getOutputValue(outputName);
			double error = expected - actualOutput;
			errorMap.put(outputName, error);
		}

		trainingAlgorithm.trainNeuralNetwork(errorMap);
		return errorMap;
	}

}
